home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / SimpleApp_Sound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-06  |  10.2 KB  |  347 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines demonstrating how to play a sound with many of the same features as QuickTime.
  5. **
  6. **    by Mark Cookson, Apple Developer Technical Support
  7. **
  8. **    File:    SimpleApp Sound.c
  9. **
  10. **    Copyright ©1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "Apple Sample
  17. **    Code" after having made changes. If you're going to re-distribute the
  18. **    source, we require that you make it clear in the source that the code
  19. **    was descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include "SimpleApp_Sound.h"
  23.  
  24. /*-----------------------------------------------------------------------*/
  25. pascal    void    MyScrollAction (ControlHandle control, short part)
  26. /*-----------------------------------------------------------------------*/
  27. {
  28.     long    position = GetControlValue (control);
  29.  
  30.     if (part) {
  31.         switch (part) {
  32.         case kInUpButtonControlPart:
  33.             position -= oneBuffer;
  34.             if (CDStyle == true) {
  35.                 if (gPlayBackwards == true) {
  36.                     ASoundPlayBackwards (mySoundInfo, true);
  37.                 }
  38.             }
  39.             break;
  40.         case kInDownButtonControlPart:
  41.             position += oneBuffer;
  42.             break;
  43.         case kInPageUpControlPart:
  44.             position -= tenBuffers;
  45.             if (CDStyle == true) {
  46.                 if (gPlayBackwards == true) {
  47.                     ASoundPlayBackwards (mySoundInfo, true);
  48.                 }
  49.             }
  50.             break;
  51.         case kInPageDownControlPart:
  52.             position += tenBuffers;
  53.             break;
  54.         default:
  55.             DebugStr ("\pWhere did you click?");
  56.         }
  57.         SetControlValue (control, position);
  58.         ASoundSetCurBuffer (mySoundInfo, position);
  59.  
  60.         if (CDStyle == true) {
  61.             ASoundSetBytesCopied (mySoundInfo, position * ASoundGetBufferSize (mySoundInfo));
  62.         }
  63.     }
  64. }
  65.  
  66. /*-----------------------------------------------------------------------*/
  67. pascal    void    DoScroll (ControlHandle control, short part)
  68. /*-----------------------------------------------------------------------*/
  69. {
  70.     long    position    = nil;
  71.     Point    pt            = {nil, nil};
  72.     OSErr    theError    = noErr;
  73.     
  74.     if (part != nil) {
  75.         switch (part) {
  76.             case kInIndicatorControlPart:
  77.                 if (CDStyle == false) {
  78.                     theError = ASoundPauseForAdjust (mySoundInfo);
  79.                 }
  80.                 GetMouse(&pt);
  81.                 part = TrackControl (control, pt, nil);
  82.                 position = GetControlValue (control);
  83.                 ASoundSetCurBuffer (mySoundInfo, position);
  84.                 ASoundSetBytesCopied (mySoundInfo, position * ASoundGetBufferSize (mySoundInfo));
  85.                 break;
  86.             case kInUpButtonControlPart:
  87.             case kInDownButtonControlPart:
  88.             case kInPageUpControlPart:
  89.             case kInPageDownControlPart: {
  90.                 ControlActionUPP actionProc = NewControlActionProc (MyScrollAction);
  91.                 if (CDStyle == false) {
  92.                     theError = ASoundPauseForAdjust (mySoundInfo);
  93.                 }
  94.                 part = TrackControl (control, pt, actionProc);
  95.                 DisposeRoutineDescriptor (actionProc);
  96.                 break;
  97.             }
  98.         }
  99.     
  100.         if (CDStyle == false) {
  101.             position = GetControlValue (control);
  102.             ASoundSetBytesCopied (mySoundInfo, position * ASoundGetBufferSize (mySoundInfo));
  103.             theError = ASoundPauseForAdjust (mySoundInfo);
  104.         }
  105.     }
  106. }
  107.  
  108. /*-----------------------------------------------------------------------*/
  109.         void    MyIdleProc (EventRecord *evt)
  110. /*-----------------------------------------------------------------------*/
  111. {
  112. #ifndef __SC__
  113. #pragma unused (evt)
  114. #endif
  115.     long        curBuf        = nil;
  116.  
  117.     if (soundPlaying == true) {
  118.         curBuf = ASoundGetCurBuffer (mySoundInfo);
  119.         if (curBuf != nil) {
  120.             SetControlValue (scrollBar, curBuf - 1);
  121.         }
  122.     }
  123.  
  124.     if (ASoundIsDone (mySoundInfo) == true) {
  125.         EnableControl (startButton);
  126.         DisableControl (stopButton);
  127.         DisableControl (resumeButton);
  128.         DisableControl (scrollBar);
  129.         SetControlValue (scrollBar, kScrollMinValue);
  130.         ASoundDonePlaying (mySoundInfo, kCloseFile + kFreeMem);
  131.         SetWTitle (gTheWindow, gOldWindowTitle);
  132.         InstallIdleProc ((EventProcs)nil);
  133.     }
  134. }
  135.  
  136. /*-----------------------------------------------------------------------*/
  137.         OSErr    DoPlay (void)
  138. /*-----------------------------------------------------------------------*/
  139. {
  140.     Str63    title;
  141.     OSErr    theErr;
  142.  
  143.     theErr = ASoundGetFileToPlay (mySoundInfo);
  144.     if (theErr == noErr) {
  145.         theErr = ASoundStartPlaying (mySoundInfo, nil);
  146.         if (theErr == noErr) {
  147.             SADisableMe();
  148.             ASoundGetSoundName (mySoundInfo, title);
  149.             SetWTitle (gTheWindow, title);
  150.             EnableControl(resumeButton);
  151.             EnableControl(stopButton);
  152.             paused = false;
  153.             InstallIdleProc ((EventProcs)MyIdleProc);
  154.             EnableControl (scrollBar);
  155.             SetControlMaximum (scrollBar, ASoundGetNumBuffers (mySoundInfo) - 1);
  156.         }
  157.     }
  158.  
  159.     return theErr;
  160. }
  161.  
  162. /*-----------------------------------------------------------------------*/
  163.         OSErr    DoPause (void)
  164. /*-----------------------------------------------------------------------*/
  165. {
  166.     Boolean            theError;
  167.     
  168.     theError = ASoundPause (mySoundInfo);
  169.  
  170.     if (paused == false) {
  171.         paused = true;
  172.         SetMyTitle ("\pResume Sound");
  173.     }
  174.     else {
  175.         paused = false;
  176.         SetMyTitle ("\pPause Sound");
  177.     }
  178.  
  179.     return theError;
  180. }
  181.  
  182. /*-----------------------------------------------------------------------*/
  183.         OSErr    DoStop (void)
  184. /*-----------------------------------------------------------------------*/
  185. {
  186.     OSErr theError;
  187.  
  188.     theError = ASoundStop (mySoundInfo);
  189.  
  190.     SADisableMe();
  191.     EnableControl(startButton);
  192.     DisableControl(resumeButton); 
  193.  
  194.     if (paused == true) {
  195.         SetControlTitle(resumeButton,"\pPause Sound");
  196.     }
  197.  
  198.     return theError;
  199. }
  200.  
  201. /*-----------------------------------------------------------------------*/
  202. pascal    short    DoPostGroupHit (long  modifiers,
  203.                                 ControlRef theControl,
  204.                                 ButtonItemRecHandle brh,
  205.                                 short item)
  206. /*-----------------------------------------------------------------------*/
  207. {
  208. #pragma unused (modifiers)
  209. #pragma unused (theControl)
  210. #pragma unused (brh)
  211.  
  212.     switch (item) {
  213.         case qtStyle:
  214.             DisableControl (checkBox);
  215.             SetControlValue (checkBox, false);
  216.             CDStyle = false;
  217.             break;
  218.         case cdStyle:
  219.             EnableControl (checkBox);
  220.             SetControlValue (checkBox, gPlayBackwards);
  221.             CDStyle = true;
  222.             break;
  223.         default:
  224.             DebugStr ("\pDoPostGroupHit got an invalid control passed to it!");
  225.     }
  226.  
  227.     return nil;
  228. }
  229.  
  230. /*-----------------------------------------------------------------------*/
  231.         short    DoPlayBackwards (long modifiers)
  232. /*-----------------------------------------------------------------------*/
  233. {
  234. #pragma unused (modifiers)
  235.  
  236.     gPlayBackwards = !gPlayBackwards;
  237.     SetControlValue (checkBox, gPlayBackwards);
  238.  
  239.     return nil;
  240. }
  241.  
  242. /*-----------------------------------------------------------------------*/
  243.         void    main (void)
  244. /*-----------------------------------------------------------------------*/
  245. {
  246.     Rect        r;
  247.     short        err;
  248.     Str255         names[] = {"\pQuickTime style",
  249.                             "\pCD style"};
  250.  
  251.     InitSimpleApp (2, kUseStandardMenu);          /* Simple App Sets up the Tool Box For us */
  252.     GetDocumentWindow (128);                     /* Get our stored window */
  253.     gTheWindow = gSACurrentWindow;
  254.     GetWTitle (gTheWindow, gOldWindowTitle);
  255.  
  256.     SetRectLocation (&r, 10, 2);                /* This Sets a rects anchor point */
  257.     SetRectDimensions (&r, 90, 20);                /* This Sets its size without changing it position */
  258.     {
  259.         long buttonID = 1;
  260.         err = InstallPushButton (&buttonID, gSACurrentWindow, "\pPlay Sound", &r, (char)0, (ButtonHitProc) DoPlay, (ButtonUpdateProc)nil);
  261.         startButton = gSACurControl;
  262.     }
  263.     
  264.     SetRectLocation (&r, 110, 2);                /* This Sets a rects anchor point */
  265.     SetRectDimensions (&r, 100, 20);            /* This Sets its size without changing it position */
  266.     {
  267.         long buttonID = 2; 
  268.         err = InstallPushButton (&buttonID, gSACurrentWindow, "\pPause Sound", &r, (char)0, (ButtonHitProc) DoPause, (ButtonUpdateProc)nil);
  269.         resumeButton  = gSACurControl;
  270.         DisableControl (resumeButton);
  271.     }
  272.     
  273.     SetRectLocation (&r, 220, 2);                /* This Sets a rects anchor point */
  274.     SetRectDimensions (&r, 90, 20);                /* This Sets its size without changing it position */
  275.     {
  276.         long buttonID = 3;
  277.         err = InstallPushButton (&buttonID, gSACurrentWindow, "\pStop Sound", &r, (char)0, (ButtonHitProc) DoStop, (ButtonUpdateProc)nil);
  278.         SADisableObject (buttonID);                    /* disable the control */
  279.         stopButton = SAGetObjectHandle(buttonID);    /* lets remember the control handle */
  280.     }
  281.  
  282.     SetRectDimensions (&r, 300, 16);
  283.     SetRectLocation (&r, 10, 30);
  284.     {
  285.      long scrollBarID  = 4;    
  286.         err = InstallScrollBar (&scrollBarID, gSACurrentWindow, "\p", &r, (char)0, (MyActionProc)DoScroll, (MyActionProc)nil, (MyActionProc)nil);
  287.         SADisableObject(scrollBarID);
  288.         scrollBar = SAGetObjectHandle(scrollBarID);        /* get the object control handle */
  289.         SetControlMinimum (scrollBar, kScrollMinValue);
  290.         SetControlMaximum (scrollBar, kScrollMinValue);
  291.         SetControlValue (scrollBar, kScrollMinValue);
  292.     }
  293.  
  294.     SetRectLocation (&r, 20, 100);
  295.     {
  296.         long checkBoxID = 5;
  297.         err = InstallCheckBox (&checkBoxID, gSACurrentWindow, "\pBackwards playing", &r, nil, false, (ButtonHitProc)DoPlayBackwards, (ButtonUpdateProc)nil);
  298.         checkBox = SAGetObjectHandle(checkBoxID);
  299.         DisableControl(checkBox);
  300.     }
  301.  
  302.     SetRectLocation (&r, 10, 60);
  303.     SetRectDimensions (&r, 130, 40);
  304.     err    = InstallRadioGroup (6,                    /* Group ID */
  305.                             gSACurrentWindow,     /* owner window */
  306.                             2,                    /* Number of items */
  307.                             "\pRadio Group",    /* Group Name */
  308.                             names,                 /* Array of button Names */
  309.                             &r,                 /* Group bounds */
  310.                             nil,                /* default item (zero based) */
  311.                             2,                     /* Horz spacing */
  312.                             2,                     /* vert spacing */
  313.                             16,                    /* Button Height */
  314.                             130,                /* Button widths */
  315.                             nil,                /* Pre Hit Proc */
  316.                             DoPostGroupHit,     /* Post Hit Proc */
  317.                             nil                    /* Group Update Proc */
  318.                             );
  319.  
  320.     GetPrintArea (gSACurrentWindow,&r);            /* Set the print area top coordinate */
  321.     r.top = 22;                                  /* so that you don't scroll the button out of view */
  322.     SetPrintArea (gSACurrentWindow,&r);
  323.     mySoundInfo = ASoundNew (&err);
  324.  
  325.     if (err == noErr)
  326.         Run ();                                 /* Let SimpleApp handle the rest */
  327. }
  328.  
  329. // utility functions since I already keep the control handle around
  330. /*-----------------------------------------------------------------------*/
  331.     void EnableControl (ControlRef cr)
  332. /*-----------------------------------------------------------------------*/
  333. {
  334.     if (cr != nil) {
  335.         HiliteControl(cr, nil);
  336.     }
  337. }
  338.  
  339. /*-----------------------------------------------------------------------*/
  340.     void DisableControl (ControlRef cr)
  341. /*-----------------------------------------------------------------------*/
  342. {
  343.     if (cr != nil) {
  344.         HiliteControl(cr, 255);
  345.     }
  346. }
  347.